package graph_viewer.util;

import java.io.*;

/**
	A helper class to make it handy to print out debug statements
 */
public final class Debug {
	static final private boolean debuggingOn	=	true;
													//false;
	
	
	//======================================
	//	public
	//======================================	
	public static final int CURRENT_LEVEL	= 58;
	
	public static void trace_bug(){		
		println( "\t !! bug-->", CURRENT_LEVEL );	
	}
	
	public static void print( String msg, int debug_level ){
		if ( !debuggingOn ){		
			return;
		}

		if ( debug_level == CURRENT_LEVEL ){
			print( msg );
			
			try{
				File_Util.append_to_file( File_Util.log, msg );				
			}catch (IOException ioe){}
		}
	}
		
	public static void println( String msg, int debug_level ){
		print( msg + "\n" ,  debug_level );
	}
	
	public static void threadPrint(String msg, int debug_level ) {
		if ( debug_level == CURRENT_LEVEL ){
			String name = Thread.currentThread().getName();

			println(name + ": " + msg);
		}
	}


	//======================================
	//	private
	//======================================	
	private static void isDebug(boolean condition) {
		if (!condition) {
		    println("isDebug Failed: ");
		    throw new IllegalArgumentException();
		}
	}

	private static void print(String msg) {
		if (debuggingOn) {
			System.out.print("--" + msg);
		}
	}

	private static void println(String msg) {
		if (debuggingOn) {
			System.out.println("--" + msg);
		}
	}

	private static void print(Exception e, String msg) {
		print((Throwable)e, msg);
	}

	private static void print(Exception e) {
		print(e, null);
	}

	private static void print(Throwable t, String msg) {
		if (debuggingOn) {
			System.out.println("Received throwable with Message: "+t.getMessage());
			if (msg != null)
				System.out.print(msg);
			t.printStackTrace();
		}
	}

	private static void print(Throwable t) {
		print(t, null);
	}

}
